import { Express, Request, Response } from "express";
import {
  addblogData,
  readBlogdata,
  getDataTotal,
  getDetail,
  delblog,
  updateBlog,
  getTypeBlog,
  getTagBlog,
} from "../model/blog";
import { Blog } from "../tool/varType";
const fs = require("fs");

export default function (app: Express, direname: string) {
  app.get("/api/getTagBlog", async function (req: Request, res: Response) {
    const { blog_Tag } = req.query as any;
    const { success, data } = await getTagBlog(blog_Tag);
    // console.log(req.query);

    if (!success) {
      res.json({ success: false, msg: data.message });
      return;
    }
    res.json({ success: true, data: data });
  });
  app.get("/api/getTypeBlog", async function (req: Request, res: Response) {
    const { blog_type } = req.query as any;
    const { success, data } = await getTypeBlog(blog_type);
    console.log(data);

    if (!success) {
      res.json({ success: false, msg: data.message });
      return;
    }
    res.json({ success: true, msg: data });
  });
  //添加博客
  app.post("/api/addBlog", async function (req: Request, res: Response) {
    const blog: Blog = req.body;
    console.log(blog);
    const { success, data } = await addblogData(blog);
    console.log(data);

    if (!success) {
      res.json({ success: false, msg: "添加失败" });
      return;
    }
    res.json({ success: true, msg: "添加成功" });
  });
  app.post("/api/getblogs", async function (req: Request, res: Response) {
    const { page } = req.body;
    let result: any = await readBlogdata(Number(page) || 1);
    if (!result.success) {
      res.json({ success: false, msg: result.data.message });
      return;
    }
    let resultTotal = await getDataTotal();
    res.json({
      success: true,
      data: { data: result.data },
      total: resultTotal.data[0].total,
    });
  });
  // 删除博客的接口
  app.post("/api/delblog", async function (req: Request, res: Response) {
    const { id } = req.body;
    let { success, data }: any = await delblog(id);
    if (!success) {
      res.json({ success: false, msg: data.message });
    }
    res.json({ success: true, msg: "删除成功" });
  });
  // 获取博客的详情
  app.get("/api/getDetail", async function (req: Request, res: Response) {
    const { id } = req.query as any;
    let { success, data }: any = await getDetail(id);
    if (!success) {
      res.json({ success: false, msg: data.message });
    }
    res.json({ success: true, data: data });
  });
  // 更新博客
  app.post("/api/updateBlog", async function (req: Request, res: Response) {
    const blog: Blog = req.body;
    let { success, data }: any = await updateBlog(blog);
    if (!success) {
      res.json({ success: false, msg: data.message });
      return;
    }
    res.json({ success: true, msg: "更新成功" });
  });
  // 文件上传接口
  // 文件上传接口
  app.post("/api/adddetailimg", function (req: Request, res: Response) {
    let name = (req as any).files[0].originalname; // 获取上传文件名称
    let path = direname + "/static/upload/" + name; // 设置文件上传的路径
    fs.readFile((req as any).files[0].path, function (err: Error, data: any) {
      if (err) {
        // 如果文件上传失败，显示错误消息到前端
        res.json({
          success: "false",
          message: "文件上传失败" + err.message,
          errno: 1,
        });
        return;
      }
      // 把读取的图片流数据写入path路径
      fs.writeFile(path, data, function (err: Error) {
        if (err) {
          // 如果文件上传失败，显示错误消息到前端
          res.json({
            success: "false",
            message: "文件上传失败" + err.message,
            errno: 1,
          });
          return;
        }
        res.json({
          success: true,
          errno: 0, // 注意：值是数字，不能是字符串
          data: {
            url: "/static/upload/" + name, // 图片 src ，必须
            alt: "", // 图片描述文字，非必须
            href: "", // 图片的链接，非必须
          },
        });
      });
    });
  });
  // 删除文件
  app.post("/api/deldetailimg", function (req: Request, res: Response) {
    const { file } = req.body;
    file.forEach(function (item: string) {
      // 拼接删除文件的路径
      item = direname + item;
      fs.unlink(item, function (err: Error) {
        if (err) {
          console.log(err);
        }
      });
    });
    res.json({ success: true, message: "删除成功" });
  });
}
